package com.easytouch.core.model;

import com.easytouch.core.utils.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * 商品模型类
 * Created by gulin on 2016/7/13.
 */
public class GoodsModel implements Serializable {
    private String mName = ""; //名称
    private String mCode = ""; //编号 10进制
    private String mImage = ""; //图片
    private String mType = ""; //类型
    private String mBarCode = ""; //条码
    private int mBox = 0; //货柜
    private int mColumn = 0; //货道

    private int mPrice = 0; //价格 分为单位
    private String mKind="";//商品分类
    private int mTemperatureState = ColumnModel.TEMPERATURE_NORMAL; //加热制冷状态
    private boolean isBreak = false;
    private int mNum = 0;
    private int mDiscount = 100; //折扣，1-100，100表示原价，1表示0.1折
    private int mWarranty = 0; //保质期，天为单位
    private int mExpiration = 0; //快过期时间，天为单位
    public String pinyin = "#";
    private static Log mLogger = new Log("GoodsModel.txt");
    public GoodsModel()
    {}

    public GoodsModel(final String pName, final String pCode, final String pImage, final String pType, final String pBarCode)
    {
        this.mName = pName;
        this.mCode = pCode;
        this.mImage = pImage;
        this.mType = pType;
        this.mBarCode = pBarCode;
    }

    public String getName() {
        return mName;
    }

    public void setName(String pName) {
        this.mName = pName;
    }

    public String getCode() {
        return mCode;
    }

    public void setCode(String pCode) {
        this.mCode = pCode;
    }

    public String getImage() {
        return mImage;
    }

    public void setImage(String pImage) {
        this.mImage = pImage;
    }

    public String getType() {
        return mType;
    }

    public void setType(String pType) {
        this.mType = pType;
    }

    public String getBarCode() {
        return mBarCode;
    }

    public void setBarCode(String pBarCode) {
        this.mBarCode = pBarCode;
    }

    public int getBox() {
        return mBox;
    }

    public void setBox(int pBox) {
        this.mBox = pBox;
    }

    public int getColumn() {
        return mColumn;
    }

    public void setColumn(int pColumn) {
        this.mColumn = pColumn;
    }

    public int getPrice() {
        return mPrice;
    }

    public void setPrice(int pPrice) {
        this.mPrice = pPrice;
    }

    public int getTemperatureState() {
        return mTemperatureState;
    }

    public void setTemperatureState(int temperatureState) {
        this.mTemperatureState = temperatureState;
    }

    public boolean isBreak() {
        return isBreak;
    }

    public void setBreak(boolean aBreak) {
        isBreak = aBreak;
    }

    public int getNum() {
        return mNum;
    }

    public void setNum(int pNum) {
        this.mNum = pNum;
    }

    public int getDiscount() {
        return mDiscount;
    }

    public void setDiscount(int pDiscount) {
        this.mDiscount = pDiscount;
    }

    public int getWarranty() {
        return mWarranty;
    }

    public void setWarranty(int pWarranty) {
        this.mWarranty = pWarranty;
    }

    public int getExpiration() {
        return mExpiration;
    }

    public void setExpiration(int pExpiration) {
        this.mExpiration = pExpiration;
    }

    public String getKind() {
        return mKind;
    }

    public void setKind(String kind) {
        mKind = kind;
    }


    public boolean isGoodsHaveStock()
    {
        int box = getBox();
        int num = getNum();
        boolean isBreak = isBreak();
        boolean isHaveStock = false;
        if (box == 11) {
            isHaveStock = !isBreak;
        } else {
            if (num > 0) {
                isHaveStock = true;
            }
        }
        return isHaveStock;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mBarCode == null) ? 0 : mBarCode.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        GoodsModel other = (GoodsModel) obj;
        if (mBarCode == null) {
            if (other.getBarCode() != null)
                return false;
        } else if (this.getBarCode().equals(other.getBarCode()) && this.getBox() == other.getBox() && this.getColumn() == other.getColumn())
            return true;
        return false;
    }

    @Override
    public String toString() {
        return "{\"name\":\"" + mName + "\",\"code\":\"" + mCode + "\",\"image\":\"" + mImage + "\",\"category\":\"" + mType + "\",\"barcode\":\"" + mBarCode + "\",\"discount\":" + mDiscount + ",\"box\":" + mBox + ",\"column\":" + mColumn + ",\"price\":" + mPrice + ",\"temperature\":" + mTemperatureState + ",\"isBreak\":" + isBreak + ",\"num\":" + mNum + ",\"warranty\":" + mWarranty + ",\"expiration\":" + mExpiration + ",\"kind\":\"" + mKind + "\"}";
    }

    /**
     * 从JSON字符串构建商品
     * @param json JSON字符串
     * @return 商品类
     */
    public static GoodsModel fromJson(final String json)
    {
        try {
            JSONObject jsonObj = new JSONObject(json);
            return fromJson(jsonObj);
        }catch (JSONException e) {
            return new GoodsModel();
        }
    }

    /**
     * 从JSON字符串构建商品
     * @param object JSON对象
     * @return 商品类
     */
    public static GoodsModel fromJson(final JSONObject object)
    {
        GoodsModel model = new GoodsModel();
        if(object != null)
        {
            try {
                model.setName(object.getString("name"));
                model.setCode(object.getString("code"));
                model.setImage(object.getString("image"));
                model.setType(object.getString("category"));
                model.setBarCode(object.getString("barcode"));
                model.setBox(object.getInt("box"));
                model.setColumn(object.getInt("column"));
                model.setPrice(object.getInt("price"));
                model.setTemperatureState(object.getInt("temperature"));
                model.setBreak(object.getBoolean("isBreak"));
                model.setNum(object.getInt("num"));
                model.setKind(object.getString("kind"));
                int discount = object.getInt("discount");
                if(discount > 0)
                {
                    model.setDiscount(discount);
                }
                int warranty = object.getInt("warranty");
                if(warranty > 0)
                {
                    model.setWarranty(warranty);
                }
                int expiration = object.getInt("expiration");
                if(expiration > 0)
                {
                    model.setExpiration(expiration);
                }
            } catch (JSONException e) {
                return model;
            }
        }
        return model;
    }

    /**
     * 从JSON字符串中判断商品是否有货
     * @param json JSON字符串
     * @return 是否有货
     */
    public static boolean isGoodsHaveStock(final String json)
    {
        JSONObject object;
        try {
            object = new JSONObject(json);
        }
        catch (JSONException e) {
            return false;
        }
        return isGoodsHaveStock(object);
    }

    /**
     * 从JSON对象中判断商品是否有货
     * @param object JSON对象
     * @return 是否有货
     */
    public static boolean isGoodsHaveStock(final JSONObject object)
    {
        if(object == null)
        {
            return false;
        }
        try {
            int box = object.getInt("box");
            int num = object.getInt("num");
            boolean isBreak = object.getBoolean("isBreak");
            boolean isHaveStock = false;
            if (box == 11) {
                isHaveStock = !isBreak;
            } else {
                if (num > 0) {
                    isHaveStock = true;
                }
            }
            return isHaveStock;
        }catch (JSONException e) {
            return false;
        }
    }

    @Override
    public GoodsModel clone() {
        return GoodsModel.fromJson(this.toString());
    }


}
